package cn.community.system.web.controller;

import cn.community.system.domain.Admin;
import cn.community.system.domain.Member;
import cn.community.system.query.AdminQuery;
import cn.community.system.service.IAdminService;
import cn.community.system.util.JsonResult;
import cn.community.system.util.UserContext;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") AdminQuery qo){
        model.addAttribute("page",adminService.query(qo));
        return "user/admin/list";
    }

    @RequestMapping("/input")
    public String input(Model model,Long id){
        if(id!=null){ //编辑
            Admin admin = adminService.get(id);
            model.addAttribute("admin",admin);
        }
        return "user/admin/input";
    }

    @RequestMapping("/profile")    // 个人中心
    @RequiresPermissions(value = {"admin/profile","超管-个人中心页面"},logical = Logical.OR)
    public String profile(Model model){
        Admin admin = UserContext.getCurrentAdmin();
        model.addAttribute("admin",adminService.get(admin.getId()));
        return "profile/profileAdmin";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"admin/saveOrUpdate","超管-个人中心编辑"},logical = Logical.OR)
    public Object saveOrUpdate(Admin admin){
        if(admin.getId() == null){ // 代表新增
            adminService.save(admin);
        }else { // 代表修改
            adminService.update(admin);
        }
        return JsonResult.success();
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids){
        adminService.batchDelete(ids);
        return JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        adminService.delete(id);
        return JsonResult.success();
    }

    @RequestMapping("/exportXls")
    @ResponseBody
    public JsonResult exportXls(Long id){

        return JsonResult.success();
    }

    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(Long id){

        return JsonResult.success();
    }

    @RequestMapping("/imgUrl")
    @ResponseBody
    public JsonResult imgUrl(){
        Admin admin = UserContext.getCurrentAdmin();
        String imgUrl = adminService.get(admin.getId()).getImgUrl();
        return JsonResult.success(imgUrl);
    }
}
